package me.jessyan.mvparms.demo.mvp.ui.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bigkoo.pickerview.view.TimePickerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;
import com.jess.arms.widget.CustomPopupWindow;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.user.DaggerPersonalDataComponent;
import me.jessyan.mvparms.demo.di.module.user.PersonalDataModule;
import me.jessyan.mvparms.demo.mvp.contract.user.PersonalDataContract;
import me.jessyan.mvparms.demo.mvp.presenter.user.PersonalDataPresenter;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;
import me.jessyan.mvparms.demo.utils.DateUtils;
import me.jessyan.mvparms.demo.utils.TimeFormatUtils;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 个人资料
 */
public class PersonalDataActivity extends BaseActivity<PersonalDataPresenter> implements PersonalDataContract.View,PopupWindow.OnDismissListener {

    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @BindView(R.id.birth_tv)
    TextView birthTv;
    @BindView(R.id.sex_tv)
    TextView sexTv;
    @BindView(R.id.head_iv)
    ImageView headIv;

    @Inject
    @Named("sexPop")
    CustomPopupWindow sexPop;
    @Inject
    @Named("choicePicPop")
    CustomPopupWindow choicePicPop;
    @Inject
    TimePickerView timePicker;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerPersonalDataComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .personalDataModule(new PersonalDataModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_personal_data; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {

            }
        });

        sexPop.setOnDismissListener(this);
        choicePicPop.setOnDismissListener(this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }



    @OnClick({R.id.head_fl, R.id.nick_name_fl, R.id.sex_fl, R.id.birth_fl})
    public void onViewClicked(View view) {
        if(DoubleClickUtils.getInstance().isDouble(view)){
            return;
        }
        switch (view.getId()) {
            case R.id.head_fl:
                if(choicePicPop!=null){
                choicePicPop.showBottom();
                backgroundAlpha(0.7f);
                }
                break;
            case R.id.nick_name_fl:
                startActivity(new Intent(this,NickNameActivity.class));
                break;
            case R.id.sex_fl:
                if(sexPop!=null){
                    sexPop.showBottom();
                    backgroundAlpha(0.7f);
                }
                break;
            case R.id.birth_fl:
                if(timePicker!=null){
                    Calendar calendar =Calendar.getInstance();
                    if(!ArmsUtils.isEmpty(birthTv.getText())) {calendar.setTimeInMillis(TimeFormatUtils.potDayFormat(birthTv.getText().toString()));}
                    timePicker.setDate(calendar);
                    timePicker.show();
                }
                break;
        }
    }

    @Override
    public PersonalDataActivity getActivity() {
        return this;
    }

    @Override
    public void popBackSex(String str) {
            sexTv.setText(str);
    }

    @Override
    public void popDismiss() {
        if(sexPop!=null&&sexPop.isShowing()){
            sexPop.dismiss();
        }
        if(choicePicPop!=null&&choicePicPop.isShowing()){
            choicePicPop.dismiss();
        }
        if(timePicker!=null&&timePicker.isShowing()){
            timePicker.dismiss();
        }

    }

    @Override
    public void popBackChoicePic(int i) {
           mPresenter.choiceHead(i);
    }

    @Override
    public void timerBackBirth(Date date) {

            birthTv.setText(TimeFormatUtils.potStandFormat(date.getTime()));
    }

    @Override
    public void popTimeBackSuccss() {
        if(timePicker!=null){

            timePicker.returnData();
            timePicker.dismiss();
        }
    }

    @Override
    public void onDismiss() {
        backgroundAlpha(1);
    }

//    https://github.com/LuckSiege/PictureSelector/blob/master/app/src/main/java/com/luck/pictureselector/adapter/GridImageAdapter.java
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                   if(selectList.size()>0){
                        LocalMedia media = selectList.get(0);
                       String path = "";
                       if (media.isCut() && !media.isCompressed()) {
                           // 裁剪过
                           path = media.getCutPath();
                       } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                           // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                           path = media.getCompressPath();
                       } else {
                           // 原图
                           path = media.getPath();
                       }

                       RequestOptions options = new RequestOptions()
                               .centerCrop()
                               .placeholder(R.color.white)
                               .diskCacheStrategy(DiskCacheStrategy.ALL);
                       Glide.with(getActivity())
                               .load(path)
                               .apply(options)
                               .into(headIv);
                   }

                    break;
            }
        }
    }
}
